package com.djhu.newscollector.crawel.imp.forumdisplay;

import com.djhu.newscollector.core.entity.Response;
import com.djhu.newscollector.crawel.core.PageProcess;
import com.djhu.newscollector.crawel.core.Url;
import com.djhu.newscollector.crawel.imp.cl.FileDownLoader;
import com.djhu.newscollector.crawel.imp.cl.entity.Img;
import com.djhu.newscollector.crawel.imp.cl.entity.ImgRequest;
import com.djhu.newscollector.crawel.imp.cl.entity.ImgResponse;
import com.djhu.newscollector.crawel.imp.worker.NewsResponse;
import com.djhu.newscollector.crawel.net.DefaultHttpVisitor;
import com.djhu.newscollector.crawel.net.HttpRequest;
import com.djhu.newscollector.crawel.net.HttpVisitor;
import com.djhu.newscollector.crawel.net.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

public class ImagePageProcess implements PageProcess {
    private static Logger logger = LogManager.getLogger(ImagePageProcess.class.getName());


    private static final String dir = "D:\\webcollector\\91";

    @Override
    public Response process(Url url) {
        NewsResponse newsResponse = new NewsResponse();
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        HttpVisitor httpVisitor = new DefaultHttpVisitor();
        Page page = null;
        try {
            page = httpVisitor.visit(request);
            ImgRequest imgRequest = new ImgRequest();
            String title = page.select("h1").text().trim();
            Elements elements = page.select("input");
            //page.select("h1").text();
            imgRequest.setDir(dir);
            imgRequest.setTitle(title);
            imgRequest.setUrl(page.url());

            List<Img> imgs = new ArrayList<>();
            imgRequest.setImg(imgs);
            for (String link:page.getLinks()) {
                //element.
                if (link.contains("attachment") && link.contains("aid") && link.contains("nothumb")){
                    Img img = new Img();
                    img.setImageurl(link);
                    imgs.add(img);

                }
            }
            FileDownLoader downLoader = new FileDownLoader();
            ImgResponse response = downLoader.download(imgRequest);

            int total = 0;
            int success = 0;
            for (Img img:response.getImgs()) {
                total++;
                if (img.isSuccess()) {
                    success++;
                }
            }

            if (total != 0){
                float percent = success/total;
                if (percent > 0.5){
                    newsResponse.setPage(page);
                }else {
                    logger.error("failed download too many ,need retey!!!" + page.url());
                    newsResponse.setException(new RuntimeException("failed download too many ,need retey"));
                }
            }
            newsResponse.setPage(page);
            //System.out.println(url.getUrl());
        }catch (Exception e) {
            e.printStackTrace();
            newsResponse.setException(e);
        }
        return  newsResponse;
    }
}
